package com.sc.controller.csm;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.domain.HouseholdLabel;
import com.sc.dto.HouseholdLabelDTO;
import com.sc.result.Result;
import com.sc.service.csm.IHouseholdLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "设置-住户标签管理")
@CrossOrigin
@RestController
@RequestMapping("/householdLabel")
@RequiredArgsConstructor
public class HouseholdLabelController {

    private final IHouseholdLabelService householdLabelService;

    @ApiOperation("新增住户标签")
    @PostMapping("/add")
    public Result add(@RequestBody HouseholdLabel householdLabel) {
        householdLabel.setAddTime(LocalDateTime.now());
        try {
            return Result.success("添加成功", householdLabelService.save((householdLabel)));
        } catch (DuplicateKeyException e) {
            return Result.error("住户标签名称重复，换一个试试");
        }
    }

    @ApiOperation("删除住户标签")
    @DeleteMapping("/deleteById")
    public Result deleteById(Integer id) {
        return Result.success("删除成功", householdLabelService.removeById(id));
    }

    @ApiOperation("批量删除住户标签")
    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Integer> ids) {
        return Result.success("删除成功", householdLabelService.removeByIds(ids));
    }

    @ApiOperation("新增住户标签")
    @PutMapping("/update")
    public Result update(@RequestBody HouseholdLabel householdLabel) {
        try {
            return Result.success("修改成功", householdLabelService.updateById(householdLabel));
        } catch (DuplicateKeyException e) {
            return Result.error("住户标签名称重复，换一个试试");
        }
    }

    @ApiOperation("获取住户标签")
    @GetMapping("/selectById")
    public Result<HouseholdLabel> selectById(Integer id) {
        return Result.success(householdLabelService.getById(id));
    }

    @ApiOperation("分页查询")
    @PostMapping("/pageQuery")
    public Result<Page<HouseholdLabel>> pageQuery(@RequestBody HouseholdLabelDTO householdLabelDTO) {
        return Result.success(householdLabelService.pageQuery(householdLabelDTO));
    }
}
